from pynput import keyboard, mouse  
import socket  
import threading,sys
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
hosts=['192.168.1.59','192.168.1.59']
port=12345
def broadcast(msg):
    client_socket.sendto(msg, (hosts[0], port)) 
    # client_socket.sendto(msg, (hosts[1], port)) 
def on_click(x, y, button, pressed):
    if(str(button)=="Button.left"):
        btn='1'
    else:
        btn='0' 
    if(pressed): 
        message= f'm{btn}{x},{y},1'
    else:
        message= f'm{btn}{x},{y},0'
    broadcast(message.encode())
def on_press(key):  
    try:  
        message= f'kc{key.char},1'
        if(str(key)=="'\\x11'"):
            message= f'kncq,1'
        if(str(key)=="'\\x17'"):
            message= f'kncw,1'
        if(str(key)=="'\\x05'"):
            message= f'knce,1'
        if(str(key)=="'\\x12'"):
            message= f'kncr,1'
        broadcast(message.encode())
        print(key.char)
    except AttributeError:  
        message= f'kn{key},1'
        broadcast(message.encode()) 
def on_release(key): 
    try:  
        message= f'kc{key.char},0'
        broadcast(message.encode())
    except AttributeError:  
        message= f'kn{key},0'
        broadcast(message.encode()) 
def on_scroll(x, y, dx, dy):  
    message= f'ms{dx},{dy}'
    broadcast(message.encode())
def on_move(x, y):  
    message= f'mm{x},{y}'
    broadcast(message.encode())
def listener1():  
    with mouse.Listener(on_move=on_move,on_click=on_click,on_scroll=on_scroll) as listener: 
        listener.join()  
def listener2():  
    with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:  
        listener.join()  
mouse_thread = threading.Thread(target=listener1)  
mouse_thread.start()
keyboard_thread = threading.Thread(target=listener2)  
keyboard_thread.start()  

